package com.jy.adscm.operation.newhscrb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.newhscrb.bean.NhcProcedure;
import com.jy.adscm.operation.newhscrb.bean.QueryNhcProcedure;
import com.jy.adscm.operation.newhscrb.mapper.mysql.NhcProcedureMapper;
import com.jy.adscm.operation.newhscrb.service.NhcProcedureService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 耗材工序 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2020-06-23
 */
@Service
public class NhcProcedureServiceImpl extends ServiceImpl<NhcProcedureMapper, NhcProcedure> implements NhcProcedureService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertNhcProcedure(NhcProcedure nhcProcedure) throws AdscmSystemException{
         //查询如果有工序编码一样的提示
         Wrapper<NhcProcedure> wrapper=new EntityWrapper<>();
         wrapper.eq("code",nhcProcedure.getCode());
         List<NhcProcedure> nhcProcedures = super.selectList(wrapper);
         if (nhcProcedures!=null && nhcProcedures.size()>0){
             throw  new AdscmSystemException("不能出现相同工序号");
         }
         boolean insert = super.insert(nhcProcedure);

         return insert;
    }

    @Override
    public boolean updateNhcProcedure(NhcProcedure nhcProcedure)throws AdscmSystemException {
        Wrapper<NhcProcedure> wrapper=new EntityWrapper<>();
        wrapper.eq("code",nhcProcedure.getCode());
        List<NhcProcedure> nhcProcedures = super.selectList(wrapper);
        if (nhcProcedures!=null && nhcProcedures.size()>0){
            throw  new AdscmSystemException("不能出现相同工序号");
        }
        boolean insert = super.updateById(nhcProcedure);

        return insert;
    }

    @Override
    public Page<NhcProcedure> select(QueryNhcProcedure queryNhcProcedure) {
         Page<NhcProcedure> page =new Page<>(queryNhcProcedure.getCurrentPage(),queryNhcProcedure.getPageSize());
         Wrapper<NhcProcedure> wrapper=new EntityWrapper<>();
         page =super.selectPage(page ,wrapper);
        page.setTotal(selectCount(wrapper));
        return page;
    }
}
